<?php
/**
 * Created by PhpStorm.
 * User: geyouwen
 * Date: 16/10/25
 * Time: 上午9:06
 */

namespace Backend\Controller;


use Vendor\Mongo\Mongo;

class DbController
{
    private $mongo;

    public function __construct()
    {
        $this->mongo = Mongo::getInstance('locationdb', 'location');
    }


    public function index()
    {

//        $location = array(
//            array(121.41174264526363, 31.21997778060331),
//            array(121.41414590454097, 31.195018148332434),
//            array(121.38736672973629, 31.144785293681693),
//            array(121.48109384155269, 31.13832065342676),
//            array(121.5253824768066, 31.15888843337971),
//            array(121.59988351440425, 31.59988351440425),
//            array(121.58649392700191, 31.25050766239894),
//        );
//        for($i=1; $i<8; $i++)
//        {
//            $data = array(
//                'id' => $i,
//                'title' => 'Location位置'.$i,
//                'location' => array(
//                    $location[$i-1][0],
//                    $location[$i-1][1],
//                )
//            );
////            var_dump($data); exit;
//            $this->mongo->insert($data);
//            echo '插入'.$i.'条数据<br/>';
//        }



//        $where = array(
//            'id' => 98
//        );
//        $update = array(
//            'name' => 33343,
//            'price' => 7777
//        );
//        $this->mongo->update($where, $update);
//        $this->mongo->inc($where, 'price', 5);
//        $where = array(
//            'id' => 97
//        );
//        $this->mongo->del($where);

        $options = array(
            'title' => array('$like' => '位置3'),
           // 'price'   => array('$gt' => 100, '$lt' => 103)
        );
//        $orders = array(
//            'id' => 'desc'
//        );
        //建立普通索引
//        $this->mongo->addIndex(array('id'=>1));
//        $this->mongo->delIndex(array('id'=>-1));
        // 分页查询 demo
//        $list = $this->mongo->page($options, '*', 1, 3,  $orders);
//        print_r($list);

        //距离计算必须建立索引 demo
        $this->mongo->addIndex(array('location' => '2d'));
        $this->mongo->addIndex(array('location' => '2dsphere'));
        $list = $this->mongo->distance( array(121.58649392700191, 31.25050766239894), $options, 2);
        print_r($list);
    }
}